<!--
 * @Author: 李天惊 uf_lee@163.com
 * @Date: 2022-12-20 14:35:28
 * @LastEditors: 李小晴 2689279066@qq.com
 * @LastEditTime: 2023-12-06 14:23:06
 * @FilePath: \grazlink20\uc_sys\src\views\Asset\Pasture\components\EditSku.vue
 * Copyright (c) 2023 by 李天惊 uf_lee@163.com, All Rights Reserved. 
-->
<script setup lang="ts">
import { Form } from '@/components/Form'
import { useForm } from '@/hooks/web/useForm'
import { PropType, reactive, watch, ref } from 'vue'
import { required } from '@/utils/formRules'
import { useCache } from '@/hooks/web/useCache'
const { wsCache } = useCache()
import { ucOrgDeptUserListApi } from '@/api/fa/user'
const myUserList: any = ref(wsCache.get('myUserList'))
const data = ref<any>({})
const props = defineProps({
  currentRow: {
    type: Object as PropType<Nullable<any>>,
    default: () => null
  }
})

const schema = reactive<FormSchema[]>([
  {
    field: 'name',
    component: 'Input',
    label: '规格名称'
  },
  {
    field: 'number',
    component: 'Input',
    label: '净含量（公斤）'
  },
  {
    field: 'note',
    component: 'Input',
    label: '备注'
  }
])

const { register, methods, elFormRef } = useForm({
  schema
})
const rules = reactive({
  name: [required],
  number: [required],
  unit: [required]
})

watch(
  () => props.currentRow,
  (currentRow) => {
    if (!currentRow) return
    const { setValues } = methods
    setValues(currentRow)
  },
  {
    deep: true,
    immediate: true
  }
)

defineExpose({
  elFormRef,
  getFormData: methods.getFormData
})

const init = async () => {
  if (props.currentRow) {
    data.value.pasture_org_id = props.currentRow?.pasture_org_id
    if (data.value.admin_dept_id) {
      const params = {
        org_id: wsCache.get('userInfo').default_org_id,
        dept_id: data.value.admin_dept_id
      }
      const res = await ucOrgDeptUserListApi(params)
        .catch(() => {})
        .finally(() => {})
      if (res) {
        myUserList.value = res.data
      }
    }
    //
  }
}
init()
</script>

<template>
  <div>
    <Form :rules="rules" @register="register" />
  </div>
</template>
